<?php

function forward404() {
  die('forward 404');
}

function forward404unless($object) {
  if(!$object){
    forward404();
  }
}

function forward() {
}

function redirect($url) {
  header('Location: ' . $url);
  exit;
}

function is_logined () {
  return isset($_SESSION['isLogined']) && $_SESSION['isLogined'] === true;
}

function get_messages_count($status = '') {
  $result = db_fetch("select count(*) as total from message" . ($status ? (" where status='" . $status ."'") : ''));
  return $result['total'];
}

function get_pages_count($status = '') {
  $result = db_fetch("select count(*) as total from page" . ($status ? (" where status='" . $status ."'") : ''));
  return $result['total'];
}

function get_articles_count($status = '') {
  $result = db_fetch("select count(*) as total from article" . ($status ? (" where status='" . $status ."'") : ''));
  return $result['total'];
}

function get_products_count($status = '') {
  $result = db_fetch("select count(*) as total from product" . ($status ? (" where status='" . $status ."'") : ''));
  return $result['total'];
}

function get_categories_count($status = '') {
  $result = db_fetch("select count(*) as total from category");
  return $result['total'];
}


function tag_select($name, $values, $default = '', $parameters = '') {
  $field = '<select name="' . $name . '"';

  if (!is_null($parameters)) $field .= ' ' . $parameters;

  $field .= '>';

  if (empty($default) && ( (isset($_GET[$name]) && is_string($_GET[$name])) || (isset($_POST[$name]) && is_string($_POST[$name])) ) ) {
    if (isset($_GET[$name]) && is_string($_GET[$name])) {
      $default = stripslashes($_GET[$name]);
    } elseif (isset($_POST[$name]) && is_string($_POST[$name])) {
      $default = stripslashes($_POST[$name]);
    }
  }

  for ($i=0, $n=sizeof($values); $i<$n; $i++) {
    $field .= '<option value="' . tep_output_string($values[$i]['id']) . '"';
    if ($default == $values[$i]['id']) {
      $field .= ' selected="selected"';
    }

    $field .= '>' . tep_output_string($values[$i]['text'], array('"' => '&quot;', '\'' => '&#039;', '<' => '&lt;', '>' => '&gt;')) . '</option>';
  }
  $field .= '</select>';

  return $field;
}

  function get_category_tree_select($name = "parent_id", $select = '', $parent_id = 0) {
    $str = '<select name="' . $name . '" id="' . $name . '">' . "\n";
    $str .= '  <option value="0">&nbsp;--&nbsp;</option>' . "\n";
    foreach (get_category_tree_select_options($parent_id) as $option) {
      $str .= '  <option value="' . $option['category']['id'] . '"' . ($select == $option['category']['id'] ? ' selected="selected"' : '') . '>' . $option['indent'] . $option['category']['name'] . '</option>' . "\n";
    }
    $str .= "</select>\n";
    return $str;
  }
  
  function get_category_tree_select_options($parent_id = 0, $indent = '') {
    $options = array();
    $categories = db_fetch_all("select * from category where parent_id = '" . $parent_id . "' order by sort_order desc");
    if ($categories) {
      foreach ($categories as $category) {
        $options[] = array('indent' => $indent, 'category' => $category);
        $options = array_merge($options, get_category_tree_select_options($category['id'], $indent . '&nbsp;&nbsp;'));
      }
    }
    return $options;
  }
  
  function get_product_images($product_id) {
    return db_fetch_all("select * from product_image where product_id='" . $product_id . "'");
  }
  
////
// Parse the data used in the html tags to ensure the tags will not break

  function tep_parse_input_field_data($data, $parse) {
    return strtr(trim($data), $parse);
  }

  function tep_output_string($string, $translate = false, $protected = false) {
    if ($protected == true) {
      return htmlspecialchars($string);
    } else {
      if ($translate == false) {
        return tep_parse_input_field_data($string, array('"' => '&quot;'));
      } else {
        return tep_parse_input_field_data($string, $translate);
      }
    }
  }
/*
  function tep_output_string_protected($string) {
    return tep_output_string($string, false, true);
  }

  function tep_sanitize_string($string) {
    $string = ereg_replace(' +', ' ', $string);

    return preg_replace("/[<>]/", '_', $string);
  }
  */